﻿/* ==============================================================================
  * Description：
  * By Se7en
  * Creation Time：[2014-01-8 11:06:39]
  * @version 1.0
  * ==============================================================================*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Model.Manager;
using Model.Manager.DataBase;
using Model.Platform.PhysicalData;

namespace BML.Infrastructure.Platform
{
    /// <summary>
    ///     系统性质日志操作
    /// </summary>
    public class SystemicLoggerOp
    {


        #region 初始化数据对象

        /// <summary>
        ///     新建日志对象按照属性名称排序输入
        /// </summary>
        /// <param name="props">
        /// CreateTime 
        /// ExMag      
        /// Id         
        /// Level      
        /// Remark     
        /// Responsible
        /// Status     </param>
        /// <returns></returns>
        public ExceptionLog NewExceptionLog(params Object[] props)
        {
            return new ExceptionLog
            {
                CreateTime = (DateTime)props[0],
                ExMag = (String)props[1],
                Id = (Guid)props[2],
                Level = (Int32)props[3],
                OtherInfo = (String)props[4],
                Remark = (String)props[5],
                Responsible = (String)props[6],
                Status = (Int32)props[7]
            };
        }

        /// <summary>
        ///     新建访问对象，按照属性名称排序输入
        /// </summary>
        /// <param name="props">
        /// Browser  
        /// Id       
        /// Ip       
        /// Others   
        /// Platform
        /// Status 
        /// VisitPath
        /// VisitTime
        /// </param>
        /// <returns></returns>
        public UserVisitLog NewUserVisitLog(params Object[] props)
        {
            return new UserVisitLog
            {
                Browser     = (String)props[0],
                HttpMethod  = (String)props[1],
                Id          = (Guid)props[2],
                Ip          = (String)props[3],
                Others      = (String)props[4],
                Platform    = (String)props[5],
                Status      = (Int32)props[6],
                VisitPath   = (String)props[7],
                VisitTime   = (DateTime)props[8],
            };
        }


        #endregion

        #region 数据库

        private const Int32 ExceptionLog = 1000;
        private const Int32 UserLog = 2000;
        public Dictionary<Int32,Int32> CountLog()
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                return new Dictionary<Int32, Int32>
                {
                    {
                        ExceptionLog,
                        db.Queryable<ExceptionLog>()
                            .Count(m => m.Status == OpertorStatusCode.New || m.Status == OpertorStatusCode.Display)
                    },
                    {
                        UserLog,
                        db.Queryable<UserVisitLog>()
                            .Count(m => m.Status == OpertorStatusCode.New || m.Status == OpertorStatusCode.Display)
                    },
                };
            }
        }


        ///     根据异常级别
        ///     记录数据库
        ///     记录文本
        ///     短信发送
        ///     通知中心平台
        /// <summary>
        ///  写入异常日志到
        /// </summary>
        /// <param name="expMsg">异常信息</param>
        /// <param name="expLeve">异常级别</param>
        /// <param name="otherInfo">其他信息</param>
        public void LoggingException(String expMsg, Int32 expLeve, String otherInfo)
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                db.GetOperator<ExceptionLog>().Create(
                    NewExceptionLog(DateTime.Now, expMsg
                    , Guid.NewGuid(), expLeve, otherInfo, "", "", OpertorStatusCode.New)
                    );
                db.SaveAllChange();
            }
        }

        /// <summary>
        ///     记录用户访问信息
        /// </summary>
        /// <param name="browser">用户浏览器版本</param>
        /// <param name="ip">请求Ip</param>
        /// <param name="visitPath">请求路径</param>
        /// <param name="httpMethod">请求类型</param>
        /// <param name="platform">平台信息</param>
        /// <param name="otherInfo">其他信息</param>
        public void LoggingUserVisitInfo(String browser, String ip,String visitPath, String httpMethod,String platform,String otherInfo)
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                db.GetOperator<UserVisitLog>().Create(
                    NewUserVisitLog(browser, httpMethod, Guid.NewGuid(), ip, otherInfo,platform, OpertorStatusCode.New, visitPath, DateTime.Now));
                db.SaveAllChange();
            }
        }

        /// <summary>
        ///     查询新建或正在处理的异常日志
        /// </summary>
        /// <returns>新日志或正在处理的日志</returns>
        public IList<ExceptionLog> QueryExpLogs()
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                return db.Queryable<ExceptionLog>().Where(m => m.Status == OpertorStatusCode.New || m.Status == OpertorStatusCode.InProgress).OrderByDescending(m => m.CreateTime).ToList();
            }
        }

        /// <summary>
        ///     查询新建或可以显示的用户访问日志
        /// </summary>
        /// <returns></returns>
        public IList<UserVisitLog> QueryVisitLogs()
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                return db.Queryable<UserVisitLog>()
                    .Where(m => m.Status == OpertorStatusCode.Display || m.Status == OpertorStatusCode.New)
                    .ToList();
            }
        }

        /// <summary>
        ///     处理异常
        /// </summary>
        /// <param name="id">异常ID</param>
        /// <param name="statue">处理结果</param>
        /// <param name="msg">留言</param>
        public void ProcessingExceptionLog(Guid id, Int32 statue, String msg)
        {
            using (IDbContext db = ContextFactory.GetPlatformConnect())
            {
                var obj = db.GetOperator<ExceptionLog>().Retrieve(id);

                /****TODO:非SQLSERVER不支持
                db.GetOperator<ExceptionLog>().BatchUpdate(m => m.Id == id, m => new ExceptionLog { Status = statue, Remark = msg });
                 * 
                 */
                obj.Status = statue;
                obj.Remark = msg;
                db.GetOperator<ExceptionLog>().Modify(obj);
                db.SaveAllChange();
            }
        }

        #endregion

        #region 发送短信

        #endregion

        #region 通知中心管理平台




        #endregion
    }
}